<?php
namespace Admin\Controller;
use Think\Controller;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/25
 * Time: 14:49
 */
class ServerController extends CommonController
{
    private $model;
    public function __construct()
    {
        parent::__construct();
        $this->model = D('Server');
    }
    /**
     * 
    * 应用列表
    * @author wang
    * @param 
    */
    public function serverList()
    {
        $where = array();
        $sort = I('request.sort', 'orderid');
        $order = I('request.order', 'asc');
        $server = I('server');
        if(!empty($server)) $where['title|description'] = array('like','%'.$server.'%');

        $result = $this->model->getList($where,$sort." ".$order);
        // $result = $this->model->getLastSql();        
        foreach ($result as $k => &$v) {
            $v['createtime']=date("Y-m-d H:i:s",$v['createtime']);
            $v['updatetime']=date("Y-m-d H:i:s",$v['updatetime']);
            $v['orderid']=(int)$v['orderid'];
        }
        $this->ajaxReturn($result);
    }

    /**
    * 增加Server
    * @author wang
    * @param 
    */
    public function addServer(){
    	$id = I('id');
        $date = I();
        $date['admin'] = session('admin');
        $date['updatetime'] = time();
    	if($id){
    		if($this->model->where("id='$id'")->save($date)){
            	$this->ajaxReturn(array('success' => 1, 'msg' => '用户修改成功','id'=>$id));
	        }else{
	            $this->ajaxReturn(array('success' => 0, 'msg' => '用户修改失败'));
	        }
    	}else{
            $date['id'] = (new  \Org\Twitter\IdWork(31,31))->nextId();
            $date['createtime']  = time();
            $date['createtim1111e']  = time();
    		if($this->model->add($date)){
            	$this->ajaxReturn(array('success' => 1, 'msg' => '用户新增成功'));
	        }else{
	            $this->ajaxReturn(array('success' => 0, 'msg' => '用户增加失败'));
	        }
    	}
    }

    /**
    * 删除Server
    * @author wang
    * @param 
    */
    public function delServer(){
        $id = I('id');
        if($this->model->where("id='$id'")->delete()){
            $result = array(
                'sta' => 1, 
                'msg' => '删除成功'
            );
        }else{
            $result = array(
                'sta' => 0, 
                'msg' => '删除失败'
            ); 
        }
        $this->ajaxReturn($result);
    }



}